package com.ruimei.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruimei.entitys.Parent;
import com.ruimei.entitys.Temp;
import com.ruimei.service.TempService;
import com.ruimei.util.TokenUtils;
import com.ruimei.vo.TempVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags={"温度检测"})
@Controller
public class TempController {

    @Resource
    TempService service;
    @Resource
    private RedisTemplate<String,String> redisTemplate;

    @PostMapping({"/temp/add"})
    @ResponseBody
    @ApiOperation("家长端上温度检测")
    public Map<String, Object> addTemp(Temp temp, HttpServletRequest request)
    {

        String  parentId = TokenUtils.getParentId(request);
        if (redisTemplate.hasKey(parentId)) {
            Parent parent = JSONObject.parseObject(redisTemplate.opsForValue().get(parentId), Parent.class);
            temp.setParentId(parentId);
            temp.setParentName(parent.getFullName());
        }
        System.out.println(temp.toString());
        Map<String, Object> map = service.addTemp(temp);
        return map;
    }

    @PostMapping({"/temp/parent"})
    @ResponseBody
    @ApiOperation("获取温度数据")
    public Map<String, Object> getTempP(String studentId)
    {
        Map<String, Object> map = service.getTempP(studentId);
        return map;
    }

    @PostMapping({"/temp/teacher"})
    @ResponseBody
    @ApiOperation("获取温度数据")
    public Map<String, Object> getTempT(TempVo tempVo)
    {
        Map<String, Object> map = service.getTempT(tempVo);
        return map;
    }



}
